package com.zhoug.mpchart.renderer;

import android.graphics.Canvas;
import android.util.Log;

import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.renderer.XAxisRendererRadarChart;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

/**
 * 解决x轴上下文字不显示的问题
 * @Author 35574
 * @Date 2021/2/19
 * @Description
 */
public class MPRadarChartXAxisRenderer extends XAxisRendererRadarChart {
    private static final String TAG = "CustomRadarChartXAxisRe";
    private static final boolean DEBUG=false;

    private RadarChart mChart;

    public MPRadarChartXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, RadarChart chart) {
        super(viewPortHandler, xAxis, chart);
        mChart = chart;
    }

    @Override
    public void renderAxisLabels(Canvas c) {
        if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled())
            return;

        final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
        if (DEBUG) {
            Log.d(TAG, "renderAxisLabels:labelRotationAngleDegrees="+labelRotationAngleDegrees);
        }
        final MPPointF drawLabelAnchor = MPPointF.getInstance(0.5f, 0.25f);

        mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
        mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
        mAxisLabelPaint.setColor(mXAxis.getTextColor());

        float sliceangle = mChart.getSliceAngle();

        // calculate the factor that is needed for transforming the value to
        // pixels
        float factor = mChart.getFactor();

        MPPointF center = mChart.getCenterOffsets();
        MPPointF pOut = MPPointF.getInstance(0,0);
        for (int i = 0; i < mChart.getData().getMaxEntryCountSet().getEntryCount(); i++) {

            String label = mXAxis.getValueFormatter().getAxisLabel(i, mXAxis);
            float angle = (sliceangle * i + mChart.getRotationAngle()) % 360f;
            if (DEBUG) {
                Log.d(TAG, "renderAxisLabels:label="+label+",angle="+angle);
            }
          /*  Utils.getPosition(center, mChart.getYRange() * factor
                    + mXAxis.mLabelRotatedWidth / 2f, angle, pOut);*/
            float labelWidth = mAxisLabelPaint.measureText(label);
            float dis=labelWidth/2;
            if(angle>270-45 && angle<270+45){
                dis=mXAxis.mLabelHeight/2.0f+5;
            }else  if(angle>45 && angle<90+45){
                dis=mXAxis.mLabelHeight+5;
            }

            Utils.getPosition(center, mChart.getYRange() * factor
                    + dis, angle, pOut);
            if (DEBUG) {
                Log.d(TAG, "renderAxisLabels:pOut=["+pOut.x+","+pOut.y+"]");
            }
            drawLabel(c, label, pOut.x, pOut.y - mXAxis.mLabelRotatedHeight / 2.f,
                    drawLabelAnchor, labelRotationAngleDegrees);
        }

        MPPointF.recycleInstance(center);
        MPPointF.recycleInstance(pOut);
        MPPointF.recycleInstance(drawLabelAnchor);
    }

    @Override
    protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
        super.drawLabel(c, formattedLabel, x, y, anchor, angleDegrees);


    }
}
